package com.example.ymm.modules1.service.impl.hl.ex;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.ex.*;
import com.example.ymm.modules1.mapper.ex.ExUserPaperMapper;
import com.example.ymm.modules1.query.hl.ex.*;
import com.example.ymm.modules1.service.dto.heng.ex.MyPaperLog;
import com.example.ymm.modules1.service.dto.heng.ex.RankUserItemDto;
import com.example.ymm.modules1.service.dto.heng.ex.UserGroupIngDto;
import com.example.ymm.modules1.service.hl.ex.*;
import com.example.ymm.util.*;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
//@Transactional
@Slf4j
@DS("slave2")
public class ExUserPaperServiceImpl extends BaseServiceImpl<ExUserPaperMapper, ExUserPaper> implements ExUserPaperService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    ExUserGroupService exUserGroupService;
    @Autowired
    private ExPublishGroupService exPublishGroupService;
    @Autowired
    private ExPublishPaperService exPublishPaperService;
    @Autowired
    private ExUserAnserService exUserAnserService;
    @Autowired
    private ExUserOptionService exUserOptionService;
    @Autowired
    private ExUserPaperService exUserPaperService;
    @Autowired
    ExUserResidueService exUserResidueService;

    @Override
    public TableList<MyPaperLog> list(ExUserPaperQueryCriteria criteria, YmPageDto pageDto) {
        getPage(pageDto);
        List<MyPaperLog> myPaperLogs = baseMapper.myPaperLogs(criteria);
        PageInfo<MyPaperLog> pageInfo = new PageInfo<>(myPaperLogs);
        TableList<MyPaperLog> tableList=new TableList(pageInfo);
        return tableList;
    }

    public List<ExUserPaper> queryAll(ExUserPaperQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExUserPaper.class, criteria);
//        predicate.orderByDesc("create_time");
        List<ExUserPaper> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public List<MyPaperLog> myPaperLogs(String userName) {
        ExUserPaperQueryCriteria criteria=new ExUserPaperQueryCriteria();
        criteria.setUserName(userName);
        return baseMapper.myPaperLogs(criteria);
    }

    @Override
    public List<RankUserItemDto> getPaperUserRank(ExUserPaperQueryCriteria criteria) {

        List<RankUserItemDto> paperUserRank = baseMapper.getPaperUserRank(criteria);
        for (int i = 0; i < paperUserRank.size(); i++) {
            RankUserItemDto userItemDto = paperUserRank.get(i);
            if(userItemDto.getUserName().equals("林珊")
            &&userItemDto.getDeptName().equals("东莞恒丽")){
                userItemDto.setUserName(userItemDto.getUserName()+"-东莞");
            }
            userItemDto.setSort(i+1);

        }
        return paperUserRank;
    }


    @Override
    public List<RankUserItemDto> getPaperTeamRank(ExUserPaperQueryCriteria criteria) {

        List<RankUserItemDto> paperUserRank = baseMapper.getPaperTeamRank(criteria);
        for (int i = 0; i < paperUserRank.size(); i++) {
            RankUserItemDto userItemDto = paperUserRank.get(i);
            userItemDto.setSort(i+1);

        }
        return paperUserRank;
    }

    /**
     * 用户面试卷
     * 自动切换到下一个模块
     */
    @Override
    public void autoCloseUserGroup() {
        Set<String> keys = redisUtil.keys(RedisKeyManager.user_paper_key + "*");
        for (String key : keys) {
            String user_paper_id = redisUtil.getString(key);
            ExUserGroupQueryCriteria c1 = new ExUserGroupQueryCriteria();
            c1.setUserPaperId(user_paper_id);
            List<ExUserGroup> exUserGroups = exUserGroupService.queryAll(c1);
            //判断是否到达关闭时间
             //是否到达了关闭时间
            UserGroupIngDto userGroupIngDto = exUserGroupService.izArriveEndTime(exUserGroups);
            if(UtilValidate.isNotEmpty(userGroupIngDto.getResult_index())){
                ExUserGroup exUserGroup = exUserGroups.get(userGroupIngDto.getResult_index());
                //通知交卷
                exPublishPaperService.shoudongTongzhi(exUserGroup.getPublishPaperId());
            }else if(userGroupIngDto.getIng_index()==null){
                //没有正在进行的模块
                redisUtil.remove(key);
            }

        }
    }


    /**
     * 面试卷 不需要计算分数
     * 用户按模块重考
     * @param userPaperId
     * @param publishGroupId
     */
    @Override
    public void userPaperBrakeGroup(String userPaperId, String publishGroupId) {
        ExUserPaper userPaper = exUserPaperService.getById(userPaperId);
        if(UtilValidate.isEmpty(userPaper)){
            throw new YmException("参数有误!");
        }
        ExUserAnserQueryCriteria c1 = new ExUserAnserQueryCriteria();
        c1.setPublishGroupId(publishGroupId);
        c1.setUserPaperId(userPaperId);
        List<ExUserAnser> exUserAnsers = exUserAnserService.queryAll(c1);
        for (ExUserAnser exUserAnser : exUserAnsers) {

            ExUserOptionQueryCriteria c2=new ExUserOptionQueryCriteria();
            c2.setUserId(exUserAnser.getUserId());
            c2.setPublishPaperId(exUserAnser.getPublishPaperId());
            c2.setPublishQuestionId(exUserAnser.getPublishQuestionId());
            List<ExUserOption> userOptions = exUserOptionService.queryAll(c2);
            for (ExUserOption exUserOption : userOptions) {
                exUserOptionService.removeById(exUserOption);
            }
            exUserAnserService.removeById(exUserAnser);
        }

        //记录重开
        ExUserResidueQueryCriteria c3=new ExUserResidueQueryCriteria();
        c3.setPublishGroupId(publishGroupId);
        c3.setUserPaperId(userPaperId);
        List<ExUserResidue> exUserResidues = exUserResidueService.queryAll(c3);
        if(UtilValidate.isEmpty(exUserResidues)){
            ExUserResidue exUserResidue = new ExUserResidue();
            exUserResidue.setUserId(userPaper.getUserId());
            exUserResidue.setUserName(userPaper.getUserName());
            exUserResidue.setPublishGroupId(publishGroupId);
            exUserResidue.setUserPaperId(userPaper.getUserPaperId());
            exUserResidue.setIsStart(0);
            exUserResidueService.saveOrUpdate(exUserResidue);

        }

    }


    @Override
    public void userPaperBrakeAll(String userPaperId) {
        ExUserPaper userPaper = exUserPaperService.getById(userPaperId);
        if(UtilValidate.isEmpty(userPaper)){
            throw new YmException("参数有误!");
        }
        ExUserAnserQueryCriteria c1 = new ExUserAnserQueryCriteria();

        c1.setUserPaperId(userPaperId);
        List<ExUserAnser> exUserAnsers = exUserAnserService.queryAll(c1);
        for (ExUserAnser exUserAnser : exUserAnsers) {

            ExUserOptionQueryCriteria c2=new ExUserOptionQueryCriteria();
            c2.setUserId(exUserAnser.getUserId());
            c2.setPublishPaperId(exUserAnser.getPublishPaperId());
            c2.setPublishQuestionId(exUserAnser.getPublishQuestionId());
            List<ExUserOption> userOptions = exUserOptionService.queryAll(c2);
            for (ExUserOption exUserOption : userOptions) {
                exUserOptionService.removeById(exUserOption);
            }
            exUserAnserService.removeById(exUserAnser);
        }
        ExUserGroupQueryCriteria c2=new ExUserGroupQueryCriteria();
        c2.setUserId(userPaper.getUserId());
        c2.setUserPaperId(userPaperId);
        List<ExUserGroup> exUserGroups = exUserGroupService.queryAll(c2);
        for (ExUserGroup exUserGroup : exUserGroups) {
            exUserGroupService.removeById(exUserGroup);
        }
        exUserPaperService.removeById(userPaper);
    }
}
